package com.es.disped.web.controller.syllabusstudent;

import java.util.List;
import java.util.Map;
import java.util.Optional;

import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.es.disped.api.syllabusstudent.SyllabusStudentRelService;
import com.es.disped.common.model.MsgModel;
import com.es.disped.common.model.PageModel;
import com.es.disped.core.controller.BaseController;
import com.es.disped.dao.model.SyllabusForeignModel;
import com.es.disped.dao.model.SyllabusStudentRel;
import com.es.disped.dao.model.SyllabusStudentRelExample;
import com.es.disped.dao.model.UserForeignModel;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;


/**
 * @author Anson<br>
 *	Copyright by EasyShare 2019<br>
 *  
 *  All right reserved<br>
 *
 *  Created on 2019年7月5日 下午1:02:25<br>
 *  名称：SyllabusStudentController.java<br>
 *  描述：教学大纲学生关系管理<br>
 */
@Api(value="教学大纲学生关系管理-课程学生")
@Controller
@RequestMapping("/manage/syllabus/student")
public class SyllabusStudentController extends BaseController{

	private final String USER_NAMESPACE="com.es.disped.dao.mapper.user.UserCustomMapper";
	private final String SYLLABUS_NAMESPACE="com.es.disped.dao.mapper.syllabus.SyllabusCustomMapper";
	private final String SYLLABUS_STUDENT_NAMESPACE="com.es.disped.dao.mapper.syllabusstudent.SyllabusStudentCustomMapper";
	
	@Autowired
	SyllabusStudentRelService syllabusStudentRelService;
	
	@ApiOperation(value="保存大纲授课学生关系对象",response=MsgModel.class,httpMethod="POST",notes="将学生加入教学计划中，表明学生所在的教学计划类别，不分班级")
	@RequiresPermissions(value={"admin:save","sys:save"},logical=Logical.OR)
	@Transactional
	@RequestMapping(value="/save",method=RequestMethod.POST)
	public @ResponseBody MsgModel save(@RequestBody @ApiParam(name="syllabusStudentRel",value="教学计划学生关系对象",required=true) SyllabusStudentRel syllabusStudentRel)
	{
		int count=0;
		if(Optional.of(syllabusStudentRel)
				.filter(stuId->syllabusStudentRel.getStudentId()!=null)
				.filter(sylId->syllabusStudentRel.getSyllabusId()!=null)
				.filter(stype->syllabusStudentRel.getStudentType()!=null)
				.isPresent())
		{
			syllabusStudentRel.setRelId(getUUID());
			if(this.isNull(syllabusStudentRel.getJoinDate()))
			{
				syllabusStudentRel.setJoinDate(this.currentDate("yyyy-MM-dd"));
			}
			UserForeignModel userForeignModel=this.getService().selectOne(
					USER_NAMESPACE+".getUserForeignModel", syllabusStudentRel.getStudentId());
			SyllabusForeignModel syllabusForeignModel=this.getService().selectOne(
					SYLLABUS_NAMESPACE+".getSyllabusForeignModel", syllabusStudentRel.getSyllabusId());
			syllabusStudentRel.setStudentName(userForeignModel.getUserName());
			syllabusStudentRel.setSyllabusName(syllabusForeignModel.getSyllabusName());
			count=syllabusStudentRelService.insertSelective(syllabusStudentRel);
		}
		return this.resultMsg(count==1?HttpStatus.OK.value():HttpStatus.EXPECTATION_FAILED.value(),
				count==1?"保存成功":"保存失败",count==1?syllabusStudentRel:"");
	}
	
	
	@ApiOperation(value="更新大纲授课学生关系对象",response=MsgModel.class,httpMethod="POST",notes="更新教学计划信息")
	@RequiresPermissions(value={"admin:update","sys:update"},logical=Logical.OR)
	@Transactional
	@RequestMapping(value="/update",method=RequestMethod.POST)
	public @ResponseBody MsgModel update(@RequestBody @ApiParam(name="syllabusStudentRel",value="教学计划学生关系对象",required=true) SyllabusStudentRel syllabusStudentRel)
	{
		int count=0;
		if(Optional.of(syllabusStudentRel)
				.filter(relId->syllabusStudentRel.getRelId()!=null)
				.isPresent())
		{
			if(!this.isNull(syllabusStudentRel.getStudentId()))
			{
				UserForeignModel userForeignModel=this.getService().selectOne(
						USER_NAMESPACE+".getUserForeignModel",syllabusStudentRel.getStudentId());
				syllabusStudentRel.setStudentName(userForeignModel.getUserName());
			}
			if(!this.isNull(syllabusStudentRel.getSyllabusId()))
			{
				SyllabusForeignModel syllabusForeignModel=this.getService().selectOne(
						SYLLABUS_NAMESPACE+".getSyllabusForeignModel",syllabusStudentRel.getSyllabusId());
				syllabusStudentRel.setSyllabusName(syllabusForeignModel.getSyllabusName());
			}
			count=syllabusStudentRelService.updateByPrimaryKey(syllabusStudentRel);
		}
		return this.resultMsg(count==1?HttpStatus.OK.value():HttpStatus.EXPECTATION_FAILED.value(),
				count==1?"更新成功":"更新失败",count==1?syllabusStudentRel:"");
	}
	
	@ApiOperation(value="删除大纲授课学生关系对象",response=MsgModel.class,httpMethod="POST",notes="删除教学计划信息")
	@RequiresPermissions(value={"admin:delete","sys:delete"},logical=Logical.OR)
	@RequestMapping(value="/delete",method=RequestMethod.POST)
	public @ResponseBody MsgModel delete(@RequestBody @ApiParam(name="syllabusStudentRel",value="教学计划学生关系对象",required=true) SyllabusStudentRel syllabusStudentRel)
	{
		int count=0;
		if(Optional.of(syllabusStudentRel)
				.filter(relId->syllabusStudentRel.getRelId()!=null)
				.isPresent())
		{
//			count=syllabusStudentRelService.deleteByPrimaryKey(syllabusStudentRel.getRelId());
			this.getService().delete(SYLLABUS_STUDENT_NAMESPACE+".deleteSyllabusStudentModel", syllabusStudentRel.getRelId());
		}
		return this.resultMsg(count>=1?HttpStatus.OK.value():HttpStatus.EXPECTATION_FAILED.value(),
				count>=1?"删除成功":"删除失败",count>=1?syllabusStudentRel:"");
	}
	
	@ApiOperation(value="查询大纲授课学生关系对象",response=MsgModel.class,httpMethod="POST",
			notes="查询教学计划信息,精确检索：关系主键，学生ID，教学大纲ID；模糊检索：学生姓名，教学大纲名称，学生类型，报名时间")
	@RequestMapping(value="/list",method=RequestMethod.POST)
	public @ResponseBody MsgModel list(@RequestBody @ApiParam(name="syllabusStudentRel",value="教学计划学生关系对象",required=false) SyllabusStudentRel syllabusStudentRel)
	{
		SyllabusStudentRelExample listExample=new SyllabusStudentRelExample();
		SyllabusStudentRelExample.Criteria criteria=listExample.createCriteria();
		
		if(!this.isNull(syllabusStudentRel.getRelId()))
		{
			criteria.andRelIdEqualTo(syllabusStudentRel.getRelId());
		}
		if(!this.isNull(syllabusStudentRel.getStudentId()))
		{
			criteria.andStudentIdEqualTo(syllabusStudentRel.getStudentId());
		}
		if(!this.isNull(syllabusStudentRel.getSyllabusId()))
		{
			criteria.andSyllabusIdEqualTo(syllabusStudentRel.getSyllabusId());
		}
		if(!this.isNull(syllabusStudentRel.getStudentName()))
		{
			criteria.andStudentNameLike("%"+syllabusStudentRel.getStudentName()+"%");
		}
		if(!this.isNull(syllabusStudentRel.getSyllabusName()))
		{
			criteria.andSyllabusNameLike("%"+syllabusStudentRel.getSyllabusName()+"%");
		}
		if(!this.isNull(syllabusStudentRel.getStudentType()))
		{
			criteria.andStudentTypeLike("%"+syllabusStudentRel.getStudentType()+"%");
		}
		if(!this.isNull(syllabusStudentRel.getJoinDate()))
		{
			criteria.andJoinDateEqualTo("%"+syllabusStudentRel.getJoinDate()+"%");
		}
		List<SyllabusStudentRel> list=syllabusStudentRelService.selectByExample(listExample);
		return this.resultMsg(!this.isNull(list)?HttpStatus.OK.value():HttpStatus.EXPECTATION_FAILED.value(),
				!this.isNull(list)?"加载成功":"查询失败",!this.isNull(list)?list:"");
	}
	
	
	@ApiOperation(value="分页加载教学大纲内学生关系信息",response=MsgModel.class,httpMethod="POST",notes="分页加载教学大纲内学生关系信息，分页参数['offset','limit']")
	@RequestMapping(value="/pages",method=RequestMethod.POST)
	public @ResponseBody MsgModel pages(@RequestBody @ApiParam(name="paramMap",value="分页参数[offset,limit]",required=true) Map<String, Object> paramMap)
	{
		SyllabusStudentRelExample listExample=new SyllabusStudentRelExample();
		PageModel<SyllabusStudentRel> pageModel=null;
		if(Optional.of(paramMap)
				.filter(offset->paramMap.containsKey("offset")!=false)
				.filter(limit->paramMap.containsKey("limit")!=false)
				.isPresent())
		{
			pageModel=syllabusStudentRelService.selectByExampleForOffsetPage(listExample,
					Integer.valueOf(paramMap.get("offset").toString()), Integer.valueOf(paramMap.get("limit").toString()));
		}
		return this.resultMsg(!this.isNull(pageModel)?HttpStatus.OK.value():HttpStatus.EXPECTATION_FAILED.value(),
				!this.isNull(pageModel)?"教学大纲内学生关系信息加载成功":"教学大纲内学生关系信息加载失败",!this.isNull(pageModel)?pageModel:"");
	}
}
